package com.example.lib_cache.viewmodel;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.lib_cache.R;
import com.example.lib_cache.model.User;
import com.example.lib_cache.network.exception.ApiException;
import com.example.lib_cache.network.exception.ErrorConsumer;
import com.example.lib_cache.network.response.ResponseResultTransformer;
import com.example.lib_cache.service.ApiService;
import com.example.lib_common.util.CheckSumBuilder;

import java.io.IOException;
import java.util.Date;

import io.reactivex.functions.Consumer;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterViewModel extends ViewModel {

    private final static String TAG = RegisterViewModel.class.getSimpleName();
    private static final MediaType FORM
            = MediaType.parse("application/x-www-form-urlencoded;charset=utf-8");

    private MutableLiveData<String> registerAccount = new MutableLiveData<>();

    private String account = "";
    private String password = "";
    private User user = new User(account,password);

    public MutableLiveData<String> getRegisterAccount() {
        return registerAccount;
    }

    public void setRegisterAccount(String registerAccount) {
        this.registerAccount.postValue(registerAccount);
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean editCheck(){
        if("".equals(account)||"".equals(password)){
            return false;
        }
        return true;
    }

    public void requestRegister(ApiService apiService, final Context context){
        user = new User(account,password);
        apiService.register(user).compose(ResponseResultTransformer.<Long>obtain())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                       requestIMAccountRegister(context);
                    }
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException ex) {

                    }
                });
    }

    public void requestIMAccountRegister(Context context){
        OkHttpClient okHttpClient = new OkHttpClient();
        String url = "https://api.netease.im/nimserver/user/create.action";


        String appKey = context.getString(R.string.IMAppKey);
        String appSecret = context.getString(R.string.IMAppSecret);
        String nonce =  "12345";
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码

        RequestBody requestBody = RequestBody.create(FORM,"'accid="+account+"&token="+password+"'");
        // 设置请求的header
        Request request = new Request.Builder()
                .url(url)
                .addHeader("AppKey", appKey)
                .addHeader("Nonce", nonce)
                .addHeader("CurTime", curTime)
                .addHeader("CheckSum", checkSum)
                .post(requestBody)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG,"请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d(TAG,"同步注册IM成功");
                setRegisterAccount(account);
                Log.d(TAG,response.toString());
            }
        });

    }






}
